/*
博客评论回复模块
 */
import { SET_BLOG_COMMENT, SET_BLOG_PAGINATION, SET_COMMENT_REPLY_BODY } from '@/store/mutation-types'
import { blogCommentReplyQuery } from '@/api/blog'
import store from '@/store'
import _ from 'lodash'

const BlogIndex = {

  state: {
    commentReplyBody: {
      type: 'B_BLOG_COMMENT',
      obj: {},
      placeholder: ''
    }, //具体的字段信息根据评论类型和回复类型，消息体属性会有差异
    comment: {},
    pagination: {}
  },

  actions: {

    /**
     * 设置新的评论回复信息
     * @param commit
     * @param comments 评论信息
     */
    setCommentReplyBody({ commit }, commentReplyBody) {
      commit(SET_COMMENT_REPLY_BODY, commentReplyBody)
    },
    /**
     * 设置新的评论回复信息
     * @param commit
     * @param comments 评论信息
     */
    setCommentDatasource({ commit }, comments) {
      commit(SET_BLOG_COMMENT, comments)
    },
    /**
     * 设置新的评论回复信息
     * @param commit
     * @param comments 评论信息
     */
    setBlogPagination({ commit }, pagination) {
      commit(SET_BLOG_PAGINATION, pagination)
    },
    /**
     * 博客评论分页查询
     * @param commit
     * @param blogId 博客编号
     * @param type 评论回复类型
     * @param currentPage 当前页码
     */
    blogCommentReplyQuery({ commit }, { blogId, type, currentPage }) {
      blogCommentReplyQuery(blogId, type, currentPage).then(function(res) {
        if (res.code === '200') {
          _.forEach(res.data.records, function(item) {
            item.loadingMoreClick = false
            item.expand = true
            item.loadPageNo = 1
          })
          commit(SET_BLOG_COMMENT, res.data)

          commit(SET_BLOG_PAGINATION, {
            onChange: currentPage => {
              store.dispatch('blogCommentReplyQuery', { blogId, type: 'B_BLOG_COMMENT', currentPage })
            },
            total: res.data.total,
            pageSize: res.data.size
          })
        }
      })
    }
  },
  mutations: {
    [SET_COMMENT_REPLY_BODY]: (state, obj) => {
      state.commentReplyBody = obj
    },
    [SET_BLOG_PAGINATION]: (state, pagination) => {
      state.pagination = pagination
    },
    [SET_BLOG_COMMENT]: (state, comment) => {
      state.comment = comment
    }
  }

}

export default BlogIndex
